package com.lanou.musicproject.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.musicproject.R;
import com.lanou.musicproject.adapter.MoreSongInfoAdapter;
import com.lanou.musicproject.bean.MoreSongInfo;
import com.squareup.picasso.Picasso;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;

import java.util.List;

/**
 * Created by 王世美 on 15/10/30.
 * 最新歌曲详细内容
 */
public class MoreSongInfoActivity extends Activity {
    private List<MoreSongInfo.AlbumInfoEntity> albumInfo;
    private List<MoreSongInfo.SonglistEntity> songlist;
    private ImageView pictureIV;
    private ImageView backIV, shareIV;
    private TextView dateTV;
    private ListView more_song_list;
    private MoreSongInfoAdapter adapter;
    private RequestQueue requestQueue;
    //    private String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0" +
//            "&method=baidu.ting.album.getAlbumInfo&format=json&album_id=";
// 分享
    final UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");
    private String path = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0" +
            "&method=baidu.ting.album.getAlbumInfo&format=json&album_id=";
    private String id;
    private String pic;
    private String date;
    private TextView numTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_song_info);
        requestQueue = Volley.newRequestQueue(this);
        numTV = (TextView) findViewById(R.id.numTV);
        pictureIV = (ImageView) findViewById(R.id.pictureIV);
        dateTV = (TextView) findViewById(R.id.dateTV);
        more_song_list = (ListView) findViewById(R.id.more_song_list);
        shareIV = (ImageView) findViewById(R.id.shareIV);
        backIV = (ImageView) findViewById(R.id.backIV);
        backIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // 接收数据
        Intent intent = getIntent();
        id = intent.getStringExtra("album_id");
        this.addData();
        // 通过分享按钮或其他方法弹出分享面板
        mController.getConfig().removePlatform(SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN);
        shareIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 是否只有已登录用户才能打开分享选择页
                mController.openShare(MoreSongInfoActivity.this, false);
            }
        });
    }

    private void addData() {
        String getUrl = path + id;
        Log.i("wang", getUrl);
        StringRequest request = new StringRequest(getUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {
                    Gson gson = new Gson();
                    MoreSongInfo result = gson.fromJson(response, MoreSongInfo.class);
                    if (result != null && (songlist = result.getSonglist()) != null) {
                        adapter = new MoreSongInfoAdapter(MoreSongInfoActivity.this, songlist);
                        more_song_list.setAdapter(adapter);
                        numTV.setText(songlist.size() + "");
                        String info = result.getAlbumInfo().getTitle();
                        // 设置分享内容
                        mController.setShareContent("很好听的专辑哟: " + info);
                    }
                    pic = result.getAlbumInfo().getPic_s1000();
                    date = result.getAlbumInfo().getPublishtime();
                    Picasso.with(MoreSongInfoActivity.this).load(pic).into(pictureIV);
                    dateTV.setText(date);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }


        });
        requestQueue.add(request);
    }
}
